package com.example.woke;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.woke1.ImageShowActivity;
import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.data.Data_fuck;
import com.woke.method.MyApp;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cn.xl.xxl.imagecache.ImageLoader;

public class CreditmapActivity extends Activity implements OnItemClickListener {

	private GridView mListView;
	private String id;
	private MyApp application;
	private int phonewith;
	private ImageLoader imageLoader;
	private String str1 = "";
	private String str2 = "";
	private ArrayList<String> maplist = new ArrayList<String>();
	private ArrayList<String> textlist = new ArrayList<String>();
	private ArrayList<Data_fuck> datalist = new ArrayList<Data_fuck>();
	private RelativeLayout mBar;
	private LayoutInflater inflater;
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_creditmap);
		
		imageLoader = new ImageLoader(this);
		
		application = (MyApp) getApplication();
		phonewith = application.screenWidth;
		
		inflater = getLayoutInflater();
		id = getIntent().getStringExtra("id");
		intview();
	}
	
	@Override
	protected void onDestroy() {
		imageLoader.clearCache();
		imageLoader.clearMemoryCache();
		super.onDestroy();
	}

	private void intview() {
		
		mBar = (RelativeLayout) findViewById(R.id.avmap_progress_bar);
		findViewById(R.id.avcreditl_map_back).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		getmaprul();
		mListView = (GridView) findViewById(R.id.avcredit_list_view);
		mListView.setOnItemClickListener(this);
	}
	
	private void getmaprul() {
		AsyncHttpClient ahc = new AsyncHttpClient();
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "quota");
		params.put("a", "getApplyImage");
		params.put("quota_id", id);// ������id
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler() {
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				try {
					String flag = response.getString("flag");
					String info = response.getString("info");
					if (flag.equals("success")) {
						JSONObject jsonObject = response.getJSONObject("data");
						JSONObject jso = jsonObject.getJSONObject("data");
						String type_name = ""+jso.getString("type_name");
						String quota_pic = ""+jso.getString("quota_pic");
						String type_name2 = ""+jso.getString("type_name2");
						String type_name1 = ""+jso.getString("type_name1");
						String quota_pic2 = ""+jso.getString("quota_pic2");
						String quota_pic1 = ""+jso.getString("quota_pic1");
						if(null != quota_pic && !quota_pic.equals("")){
							maplist.add(quota_pic);
							textlist.add(type_name);
						}
						if(null != quota_pic1 && !quota_pic1.equals("")){
							maplist.add("http://"+quota_pic1);
							textlist.add(type_name1);
						}
						if(null != quota_pic2 && !quota_pic2.equals("")){
							maplist.add("http://"+quota_pic2);
							textlist.add(type_name2);
						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				
				if(maplist.size() > 0){
					Myadter adapter = new Myadter();
					mListView.setAdapter(adapter);
				}
				
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
				Toast.makeText(CreditmapActivity.this, "数据失败！",
						Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onStart() {
				super.onStart();
				mBar.setVisibility(View.VISIBLE);
			}

			@Override
			public void onFinish() {
				super.onFinish();
				mBar.setVisibility(View.GONE);
			}
		});
	}

	class Myadter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return maplist.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}
		
		
		class Holder {
			ImageView mIicon;
			RelativeLayout mTbankname;
//			TextView mTbankcarno;
			TextView mView;
//			RelativeLayout RelativeLayout;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Holder holder = null;
			if(convertView == null){
				holder = new Holder();
				convertView = inflater.inflate(R.layout.item_single_map, null);
				holder.mView = (TextView) convertView.findViewById(R.id.item_map_text);
				holder.mIicon = (ImageView) convertView.findViewById(R.id.item_map_image);
				holder.mTbankname = (RelativeLayout) convertView.findViewById(R.id.item_map_relative);
				LayoutParams params = holder.mTbankname.getLayoutParams();
				params.width = phonewith / 2;
				params.height = phonewith / 2;
				holder.mTbankname.setLayoutParams(params);
				convertView.setTag(holder);
			}else{
				 holder = (Holder) convertView.getTag();
			}
			
			imageLoader.DisplayImage("http://"+maplist.get(position), holder.mIicon, false, false, true,0);
			holder.mView.setText(textlist.get(position));	
			
			
			return convertView;
		}
		
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent intent = new Intent(CreditmapActivity.this,ImageShowActivity.class);
		intent.putExtra("index", maplist.get(arg2));
		intent.putExtra("infos", maplist);
		startActivity(intent);
	}
	
}
